<template>
  <div
    class="c-wrapper"
    v-infinite-scroll="load"
    :infinite-scroll-disabled="disabled"
  >
    <div class="container">
      <div class="header d-flex">
        <h2 class="type-now">全部分类</h2>
        <el-button
          round
          v-for="c in categoryList"
          @click="searchByCategory(c.id)"
          :class="{ active: activeBtn == c.id }"
          :key="c.id"
          >{{ c.name }}</el-button
        >
        <el-input
          prefix-icon="el-icon-search"
          v-model.trim="page.kw"
          @input="handleSearch"
          placeholder="请输入内容"
        ></el-input>
        <router-link style="margin-left: 10px" :to="{ name: 'createForum' }">
          <el-button icon="el-icon-plus" type="primary" round
            >创建板块</el-button
          ></router-link
        >
      </div>

      <div class="content">
        <el-card
          class="box-card"
          @click.native="toForum(item.id)"
          v-for="item in forumList"
          :key="item.id"
        >
          <el-image
            style="width: 100%; height: 100px"
            :src="item.avatar"
            fit="contain"
          >
          </el-image>
          <h2 class="title">{{ item.forumName }}</h2>

          <div class="detail">
            <span class="following-count">关注: {{ item.subscribe }}</span>
            &nbsp;&nbsp;&nbsp;
            <span class="post-count">帖子: {{ item.postCount }}</span>
          </div>
        </el-card>
      </div>
      <p class="footer-tips" v-if="loading">
        加载中&nbsp;<i class="el-icon-loading"></i>
      </p>

      <p class="footer-tips" v-if="noMore" style="text-align: center">
        没有更多了
      </p>
    </div>
  </div>
</template>

<script>
import forumApi from "@/axios/forumApi";
import statusCode from "@/commons/statusCode";
import debounce from "@/commons/debounce";
export default {
  data() {
    return {
      activeBtn: null,
      categoryList: [],
      page: {
        current: 1,
        size: 15,
        total: 0,
        pages: 0,
        kw: "",
      },
      loading: true,
      category: null,
      forumList: [],
    };
  },
  computed: {
    noMore() {
      return (
        (this.page.current == this.page.pages || this.page.pages == 0) &&
        !this.loading
      );
    },
    disabled() {
      return this.loading || this.noMore;
    },
  },
  methods: {
    getForumList() {
      //开启加载动画
      this.loading = true;
      //获取板块列表
      forumApi
        .getForumList({ ...this.page, category: this.category })
        .then(({ data }) => {
          console.log(data);
          if (data.code === statusCode.SUCCESS_CODE) {
            this.forumList = this.forumList.concat(data.data.records);
            this.page.total = data.data.total;
            this.page.pages = data.data.pages;
            //关闭加载动画
            this.loading = false;
          } else this.$message.error("获取数据出错");
        })
        .catch((e) => {
          this.$message.error("获取数据出错");
        });
    },
    getCategories() {
      forumApi
        .getCategories()
        .then(({ data }) => {
          if (data.code === statusCode.SUCCESS_CODE)
            this.categoryList = data.data;
          else this.$message.error("获取数据出错");
        })
        .catch((e) => {
          this.$message.error("获取数据出错");
        });
    },
    load() {
      if (this.noMore) return;
      this.page.current++;
      this.getForumList();
    },

    handleSearch: debounce(
      500,
      function () {
        this.getForumList();
      },
      function () {
        this.reset();
      }
    ),
    reset() {
      this.loading = true;
      this.page.current = 1;
      this.page.total = 0;

      this.forumList = [];
    },
    searchByCategory(id) {
      if (this.activeBtn == id) {
        this.reset();
        this.category = null;
        this.activeBtn = null;
      } else {
        this.reset();
        this.category = id;
        this.activeBtn = id;
      }
      this.loading = true;
      this.getForumList();
    },
    toForum(id) {
      let pathInfo = this.$router.resolve({
        name: "forum",
        params: { id },
      });
      window.open(pathInfo.href, "_blank");
    },
  },
  created() {
    //获取分类列表
    this.getCategories();

    //获取板块列表
    this.getForumList();
  },
};
</script>

<style lang="less" scoped>
@import "@/assets/less/common.less";
.c-wrapper {
  height: 100%;
  overflow: scroll;
}
.container {
  width: 1200px;
  margin: 0 auto;
  margin-top: 20px;
  padding-bottom: 40px;
}
.header {
  line-height: 40px;
  .type-now {
    font-weight: 400;
    margin-right: 20px;
  }
  .el-input {
    width: 250px;
    margin-left: 20px;
  }
}
.el-button:focus {
  color: #606266;
  background: #fff;
  border: 1px solid #dcdfe6;
}
.el-button.active {
  color: #409eff;
  border-color: #c6e2ff;
  background-color: #ecf5ff;
}

.content {
  display: flex;
  margin-top: 50px;
  // margin:0 10px 10px 0;
  // flex-direction: column;
  justify-content: flex-start;
  flex-wrap: wrap;

  .el-card {
    width: 210px;
    height: 210px;
    margin-bottom: 40px;
    margin-right: 30px;
    cursor: pointer;
    &:nth-child(5n) {
      margin-right: 0;
    }
    .title {
      font-weight: 400;
      text-align: center;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .detail {
      text-align: center;
      margin-top: 10px;
      font-size: 14px;
      color: @regularText;
    }
  }
}
.footer-tips {
  font-size: 20px;
  margin-bottom: 10px;
  line-height: 40px;
  text-align: center;
  color: var(--text-primary);
}
</style>